package com.indigon.parkStory.data.objects
{
	import flash.geom.Point;

	public class LillyData
	{
		public static const TYPE_STATIC    : String = "static";
		public static const TYPE_FADE      : String = "fade";
		public static const TYPE_SINGLE    : String = "single";
		public static const TYPE_BOUNDED   : String = "bound";
		public static const TYPE_TURTLE    : String = "turtle";
		public static const TYPE_CARRIER   : String = "carrier";
		public static const TYPE_NORMAL    : String = "normal";
		
		public static const CONTENT_STAR   : String = "star";
		public static const CONTENT_GOAL   : String = "goal";
		public static const CONTENT_FROG   : String = "frog";
		
		private static const MASS_MULTILIER : Number = 1.0;
		
		public var pos        : Point;
		public var radius     : Number;
		public var mass       : Number;
		
		public var type       : String;
		public var collect    : String;
		
		public var hasFrog    : Boolean;
		public var hasStar    : Boolean;
		public var hasGoal    : Boolean;
		
		public var isCarrier  : Boolean;
		public var isStatic   : Boolean;
		public var isFading   : Boolean;
		public var isSingle   : Boolean;
		public var isBounded  : Boolean;
		public var isTurtle   : Boolean;
		
		public var boundRange : Number;
		
		public var carrierPos   : Point;
		public var carrierForce : Number;
		public var carrierAng   : Number;
		
		public function LillyData(xml:XML)
		{
			this.pos    = new Point(Number(xml.@x), Number(xml.@y));
			this.radius = Number(xml.@radius);
			this.type   = String(xml.@type);
			
			this.hasFrog = String(xml.@content) == CONTENT_FROG ? true : false;
			this.hasStar = String(xml.@content) == CONTENT_STAR ? true : false;
			this.hasGoal = String(xml.@content) == CONTENT_GOAL ? true : false;
			
			this.isCarrier = String(xml.@type) == TYPE_CARRIER ? true : false;
			this.isTurtle  = String(xml.@type) == TYPE_TURTLE  ? true : false;
			this.isBounded = String(xml.@type) == TYPE_BOUNDED ? true : false;
			this.isStatic  = String(xml.@type) == TYPE_STATIC  ? true : false;
			this.isFading  = String(xml.@type) == TYPE_FADE    ? true : false;
			this.isSingle  = String(xml.@type) == TYPE_SINGLE  ? true : false;
			if (type == null)
			{
				this.type = TYPE_NORMAL;
			}
			
			if (isBounded)
			{
				this.boundRange = Number(xml.@boundRange);
			}
			else if (isCarrier)
			{
				this.carrierPos = new Point(Number(xml.@centerX), Number(xml.@centerY));	
				this.carrierForce = Number(xml.@force);
				this.carrierAng = Number(xml.@angle);
			}
			
			this.mass = isStatic ? 0.0 : (radius * radius * Math.PI * MASS_MULTILIER);
		}
		
		public function toXML():XML
		{
			var xml : XML = <lilly/>;
				
			xml.@x 		 = this.pos.x;
			xml.@y 		 = this.pos.y;
			xml.@radius  = this.radius;
			
			if (this.hasFrog)
			{
				xml.@content = CONTENT_FROG;
			}
			else if (this.hasGoal)
			{
				xml.@content = CONTENT_GOAL;
			}
			else if (this.hasStar)
			{
				xml.@content = CONTENT_STAR;
			}
			
			xml.@type = this.type;
			
			if (this.type == TYPE_BOUNDED)
			{
				xml.@boundRange = this.boundRange;
			}
			else if (this.type == TYPE_CARRIER)
			{
				xml.@angle	  = this.carrierAng;
				xml.@velocity = this.carrierForce;
				xml.@centerX  = this.carrierPos.x;
				xml.@centerY  = this.carrierPos.y;
			}
			
			return xml;
		}
	}
}